package com.xc.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.demo.common.Constants;
import com.xc.demo.entity.Employee;
import com.xc.demo.entity.Menu;
import com.xc.demo.exception.ServiceException;
import com.xc.demo.mapper.EmployeeMapper;
import com.xc.demo.mapper.RoleMapper;
import com.xc.demo.mapper.RoleMenuMapper;
import com.xc.demo.service.EmployeeService;
import com.xc.demo.service.MenuService;
import com.xc.demo.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>  implements EmployeeService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private MenuService menuService;


    @Override
    public Employee login(Employee employee) {

        String password = employee.getPassword();

        password = DigestUtils.md5DigestAsHex(password.getBytes());

        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",employee.getUsername());
        queryWrapper.eq("password",password);
       Employee emp;
       try{
           emp = getOne(queryWrapper);
       }catch (Exception e){
           log.error(String.valueOf(e));
           throw new ServiceException(Constants.CODE_500,"系统错误");
       }

           if(emp !=null){
               if(emp.getStatus() == 0){
                   throw  new ServiceException(Constants.CODE_401,"该用户已注销");
               }
               if(emp.getStatus() == 2){
                   throw  new ServiceException(Constants.CODE_401,"该用户待审核中");
               }
//               设置token
               String token= TokenUtils.genToken(emp.getId().toString(),emp.getPassword());
               employee.setToken(token);

               String role= emp.getRole();



//             设置用户的菜单列表
               List<Menu> roleMenus = getRoleMenus(role);

               employee.setMenus(roleMenus);


               return employee;
           }

           else {
                   throw new ServiceException(Constants.CODE_600,"用户名或密码错误");
           }


    }


    private List<Menu> getRoleMenus(String roleFlag){

        Integer roleId = roleMapper.selectByFlag(roleFlag);


        //               当前角色的所有菜单id的list
         List<Integer> menuIds = roleMenuMapper.selectByRoleId(roleId);


//               查出系统所有的菜单
        List<Menu>  menus= menuService.findMenus("");

//                新建一个筛选完成后的List
        List<Menu> roleMenus = new ArrayList<>();


        roleMenus = menuService.listByIds(menuIds);

//                筛选当前用户角色的菜单
//        for(Menu menu:menus){
//
//            if (menuIds.contains(menu.getId())){
//                roleMenus.add(menu);
//            }
//
//            List<Menu> children = menu.getChildren();
////                   移除children中不在menuIds集合的元素
//            children.removeIf(child -> !menuIds.contains(child.getId()));
//
//        }

//        log.info(String.valueOf(roleMenus));
        return  roleMenus;
    }

}
